package com.mkl.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkl.system.common.Constant;
import com.mkl.system.common.DataGridView;
import com.mkl.system.domain.Menu;
import com.mkl.system.mapper.MenuMapper;
import com.mkl.system.mapper.RoleMapper;
import com.mkl.system.service.MenuService;
import com.mkl.system.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mkl
 * @create 2020/2/28 11:21
 * @Description
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Menu> queryAllMenuForList() {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        qw.and(menuQueryWrapper -> menuQueryWrapper
                .eq("type", Constant.MENU_TYPE_TOP)
                .or().eq("type", Constant.MENU_TYPE_LEFT));
        qw.orderByAsc("ordernum");
        return menuMapper.selectList(qw);
    }

    /**
     * @param id 用户id
     * @return
     */
    @Override
    public List<Menu> queryMenuForListByUserId(Integer id) {
        //根据用户id查询角色id的集合
        List<Integer> roleIds = roleMapper.queryRoleIdsByUserId(id);
        //根据角色id的集合，查询菜单的id集合
        if (null != roleIds && roleIds.size() > 0) {
            List<Integer> menuIds = roleMapper.queryMenuIdsByRids(roleIds);
            if (null != menuIds && menuIds.size() > 0) {
                QueryWrapper<Menu> qw = new QueryWrapper<>();
                qw.eq("available", Constant.AVAILABLE_TRUE);
                qw.and(mqw -> mqw
                        .eq("type", Constant.MENU_TYPE_TOP).or()
                        .eq("type", Constant.MENU_TYPE_LEFT));
                qw.in("id", menuIds);
                qw.orderByAsc("ordernum");
                return menuMapper.selectList(qw);
            }

        }
        return new ArrayList<>();
    }

    @Override
    public DataGridView queryAllMenu(MenuVo menuVo) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq(menuVo.getAvailable() != null, "available", menuVo.getAvailable());
        qw.orderByAsc("ordernum");
        List<Menu> menus = menuMapper.selectList(qw);
        return new DataGridView((long) menus.size(), menus);
    }

    @Override
    public void saveMenu(Menu menu) {
        menuMapper.insert(menu);
    }

    @Override
    public void updateMenu(Menu menu) {
        menuMapper.updateById(menu);
    }

    @Override
    public Integer queryMenuChildrenCountById(Integer id) {
        return menuMapper.queryMenuChildrenCountById(id);
    }

    @Override
    public Integer queryMenuMaxOrderNum() {
        return menuMapper.queryMenuMaxOrderNum();
    }

    @Override
    public List<String> queryPermissionCodesByUserId(Integer id) {
        //根据用户id查询角色id的集合
        List<Integer> roleIds = roleMapper.queryRoleIdsByUserId(id);
        //根据角色id的集合，查询菜单的id的集合
        if (null != roleIds && roleIds.size() > 0) {
            List<Integer> menuIdsByRids = roleMapper.queryMenuIdsByRids(roleIds);
            if (null != menuIdsByRids && menuIdsByRids.size() > 0) {
                QueryWrapper<Menu> qw = new QueryWrapper<>();
                qw.eq("available", Constant.AVAILABLE_TRUE);
                qw.eq("type", Constant.MENU_TYPE_PERMISSION);
                qw.in("id", menuIdsByRids);
                qw.orderByAsc("ordernum");
                List<Menu> menus = menuMapper.selectList(qw);
                List<String> permissions = new ArrayList<>();
                for (Menu menu : menus) {
                    permissions.add(menu.getTypeCode());
                }
                return permissions;
            }
        }
        return new ArrayList<>();
    }
}
